package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.AlbumDao;
import cn.sumpu.app.guoguojie.domain.Album;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class AlbumDaoImpl extends SqlSessionDaoSupport implements AlbumDao {

	private final static Logger logger = LoggerFactory
			.getLogger(AlbumDaoImpl.class);

	@Override
	public void insert(Album album) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.AlbumMapper.insert",
					album);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Insert database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Album> getList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<Album>) getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.AlbumMapper.getList",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}
	
	

	@Override
	public int getListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.AlbumMapper.getListCount",
					map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getListCount failure.", e);
			}
			throw new AppBusinessException("getListCount failure.", e);
		}
	}

	@Override
	public void update(Album album) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.AlbumMapper.update",
					album);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Update database failure.", e);
			}
			throw new AppBusinessException("Update database failure.", e);
		}
	}

	@Override
	public void deleteById(String id) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.AlbumMapper.deleteById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Delete database failure.", e);
			}
			throw new AppBusinessException("Delete database failure.", e);
		}
	}

	@Override
	public Album getById(String id) throws AppBusinessException {
		try {
			return (Album) getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.AlbumMapper.getById",
					id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Album> getAlbumList(String family) throws AppBusinessException {
		try {
			return  getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbumList",family);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAlbumList failure.", e);
			}
			throw new AppBusinessException("getAlbumList failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Album> getAlbumFriend(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return  getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbumFriend",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAlbumFriend failure.", e);
			}
			throw new AppBusinessException("getAlbumFriend failure.", e);
		}
	}

	@Override
	public int getAlbumFriendCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.AlbumMapper.getAlbumFriendCount",
					map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAlbumFriendCount failure.", e);
			}
			throw new AppBusinessException("getAlbumFriendCount failure.", e);
		}
	}
	
	

}
